package com.dmp.util;

import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sunzy
 *
 */
public class UrlCRequest {

	/**
	 * 解析出url请求的路径，包括页面
	 * 
	 * @param strURL
	 *            url地址
	 * @return url路径
	 */
	public static String urlPage(String strURL) {
		String strPage = null;
		String[] arrSplit = null;

		// strURL = strURL.trim().toLowerCase();

		arrSplit = strURL.split("\\?");
		if (strURL.length() > 0) {
			if (arrSplit.length > 1) {
				if (arrSplit[0] != null) {
					strPage = arrSplit[0];
				}
			}
		}

		return strPage;
	}

	/**
	 * 去掉url中的路径，留下请求参数部分
	 * 
	 * @param strURL
	 *            url地址
	 * @return url请求参数部分
	 */
	private static String truncateUrlPage(String strURL) {
		String strAllParam = null;
		String[] arrSplit = null;

		arrSplit = strURL.split("[?]");
		if (strURL.length() > 1) {
			if (arrSplit.length > 1) {
				if (arrSplit[1] != null) {
					strAllParam = StringUtils.substring(arrSplit[1], 0 , -9);
				}
			}
		}

		return strAllParam;
	}

	/**
	 * 解析出url参数中的键值对 如 "index.jsp?Action=del&id=123"，解析出Action:del,id:123存入map中
	 * 
	 * @param URL
	 *            url地址
	 * @return url请求参数部分
	 */
	public static Map<String, String> urlRequest(String url) {
		Map<String, String> mapRequest = new HashMap<String, String>();

		String[] arrSplit = null;

		String strUrlParam = truncateUrlPage(url);
		if (strUrlParam == null) {
			return mapRequest;
		}
		// 每个键值为一组
		arrSplit = strUrlParam.split("[&]");
		for (String strSplit : arrSplit) {
			String paramKey = StringUtils.substring(strSplit, 0, strSplit.indexOf("="));
			String paramValue = StringUtils.substring(strSplit, strSplit.indexOf("=") + 1);
					
			if(StringUtils.isNotBlank(paramKey)){
				if(StringUtils.isNotBlank(paramValue)){
					mapRequest.put(paramKey, paramValue);
				} else {
					mapRequest.put(paramKey, "");
				}
			}
		}
		
		return mapRequest;
	}


	/** 
	  * @author  sunzy
	  * @date 2016年4月21日 上午11:48:26 
	  * @Description 解析url的params，返回map
	  *
	  * @param params
	  * @return
	  * @return  Map<String,String>
	  */
	public static Map<String,String> getUrlParamsMap(String params){
		Map<String, String> mapRequest = new HashMap<String, String>();

		String[] arrSplit = null;

		String strUrlParam = params;
		if (strUrlParam == null) {
			return mapRequest;
		}
		// 每个键值为一组
		arrSplit = strUrlParam.split("[&]");
		for (String strSplit : arrSplit) {
			String paramKey = StringUtils.substring(strSplit, 0, strSplit.indexOf("="));
			String paramValue = StringUtils.substring(strSplit, strSplit.indexOf("=") + 1);
					
			if(StringUtils.isNotBlank(paramKey)){
				if(StringUtils.isNotBlank(paramValue)){
					mapRequest.put(paramKey, paramValue);
				} else {
					mapRequest.put(paramKey, "");
				}
			}
		}
		
		return mapRequest;
	}
	
	/**
	 * 获取解码后的串 by sunzy
	 * 
	 * @param code
	 *            加密串
	 * @return String 解密串
	 */
	public static String getCode(String code) {
		String deCode = "";
		int flag = 0;
		String lastFour = "";
		String beforeString = "";
		String reslutString = "";
		String strOut2 = "";
		

		if (code == null || code.equals("")) {
			flag = 1;
		}
		if (flag == 0) {
			lastFour = code.substring(code.length() - 4, code.length());
			beforeString = code.substring(0, code.length() - 4);
			reslutString = lastFour + beforeString;
			strOut2 = new String(Base64Util.decode(reslutString));
			System.out.println(strOut2);
			deCode = strOut2.trim();
		}
		String[] tirmedDeCode = deCode.split("-");
		
		deCode = tirmedDeCode[(tirmedDeCode.length -1)];
		return deCode;
	}
	
	 // 解密  
    @SuppressWarnings("restriction")
	public static String getFromBase64(String s) {  
        byte[] b = null;  
        String result = null;  
        if (s != null) {  
			BASE64Decoder decoder = new BASE64Decoder();  
            try {  
                b = decoder.decodeBuffer(s);  
                result = new String(b, "utf-8");  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
        return result;  
    } 

	public static Map<String, String> decodedRequest2Map(String termedEncodeRequest) {
		Map<String, String> mapRequest = new HashMap<String, String>();

		String[] arrSplit = null;

		if (termedEncodeRequest == null) {
			return mapRequest;
		}
		// 每个键值为一组
		arrSplit = termedEncodeRequest.split("[&]");
		for (String strSplit : arrSplit) {
			String paramKey = StringUtils.substring(strSplit, 0, strSplit.indexOf("="));
			String paramValue = StringUtils.substring(strSplit, strSplit.indexOf("=") + 1);
					
			if(StringUtils.isNotBlank(paramKey)){
				if(StringUtils.isNotBlank(paramValue)){
					mapRequest.put(paramKey, paramValue);
				} else {
					mapRequest.put(paramKey, "");
				}
			}
		}
		
		return mapRequest;
	}
    
	/**
	 * 去掉加密后的url中的路径和末尾，解密后重新组装
	 * 
	 * @param strURL
	 *            url地址
	 * @return url请求参数部分
	 */
	public static String decodeUrlPage(String strURL) {
		String decodedstrUrl = strURL;
		String[] arrSplit = null;
		String[] endSplit = null;
		String[] urlSplit = new String[3];
		// strURL = strURL.trim().toLowerCase();

		arrSplit = strURL.split("[?]");
		if (strURL.length() > 1) {
			if (arrSplit.length == 2) {
				if (arrSplit[1] != null) {
					System.out.println(arrSplit[0]);
					System.out.println(arrSplit[1]);
					endSplit = arrSplit[1].split("[\\s]");
					System.out.println(endSplit);
					urlSplit[0]=arrSplit[0];
					urlSplit[1]=getFromBase64(endSplit[0]);
					urlSplit[2]=endSplit[1];
						decodedstrUrl = urlSplit[0] + "?" + urlSplit[1] + " "+ urlSplit[2];					
				}
			}
		}		
		return decodedstrUrl;
	}

	public static void main(String...args){
		String a = "dXRpbWU9MTQ2NjUwMDI5NzUwOCZpc01vYmxpZT0wJnU9MTYzMTM5MDEwMDEwMDQxMCZhPTA4OTgyMzMyMjkxNyZhZF9pZD0yNSZzdD0xNDY2NTAxMDg2MzgwJmRlbWFuZF9pZD0yNyZwb2xpY3lfaWQ9MjE=";
		a = UrlCRequest.getFromBase64(a);
		System.err.println(a);
	}
}
